package com.qiyigu.itc.speech;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;

import com.qiyigu.itc.base.aidl.AidlBinderPoolClient;
import com.qiyigu.itc.base.bean.SpeechMessageBean;
import com.qiyigu.itc.base.service.ISpeechService;
import com.qiyigu.itc.speech.service.BaseServiceImpl;
import com.qiyigu.itc.speech.service.WarkUpService;
import com.qiyigu.itc.speech.service.listener.OnSdkWarkUpListener;
import com.qiyigu.itc.speech.service.listener.OnSpeechListener;
import com.qiyigu.itc.speech.baidu.BaiduSpeechParserServiceImpl;
import com.qiyigu.itc.speech.baidu.BaiduWarkupServiceImpl;
import com.qiyigu.itc.speech.xunfai.XunfaiSpeechParserServiceImpl;
import com.qiyigu.itc.speech.xunfai.XunfaiSpeechServiceImpl;

public class SpeechServiceImpl extends BaseServiceImpl implements OnSpeechListener,
        OnSdkWarkUpListener {

    protected static SpeechServiceImpl service = null;

    private ISpeechService mISpeechService = null;

    protected SpeechService mySpeechService;
    protected WarkUpService myWarkUpService;
    private boolean isSpeechIng = false;

    private static final int END_WARKUP = 0X12301;
    private static final int DELAY_END_WARKUP = 5000;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == END_WARKUP) {
                SpeechServiceImpl.this.onEndWarkUp();
            }
        }
    };

    protected SpeechServiceImpl() {
        service = this;
        //讯飞的唤醒 词 与aiui集成有问题
        // myWarkUpService = XunfaiWarkUpServiceImpl.getInstance(this);
        myWarkUpService = BaiduWarkupServiceImpl.getInstance(this,this);
        mySpeechService = XunfaiSpeechServiceImpl.getInstance(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                mISpeechService =  AidlBinderPoolClient.getInstance(MyApp.getInstance()).getISpeechService();
            }
        }).start();
    }

    public static synchronized SpeechServiceImpl getInstance() {
        if (service == null) {
            service = new SpeechServiceImpl();
        }
        return service;
    }


    public synchronized void start() {
        myWarkUpService.startWarkUp();
    }


    public synchronized void stop() {
        myWarkUpService.stopWarkUp();
        mySpeechService.stopSpeech();
    }


    public synchronized void release() {
        myWarkUpService.releaseWarkUp();
        mySpeechService.releaseSpeech();
    }

    @Override
    public void onWarkUp() {
        //TODO 调界面显示
        logger.i("onWarkUp success 调界面显示");
        SpeechDialogView.getInstance(MyApp.getInstance()).openView();
        mHandler.removeMessages(END_WARKUP);
        mHandler.sendEmptyMessageDelayed(END_WARKUP, DELAY_END_WARKUP);
    }

    @Override
    public void onEndWarkUp() {
        mySpeechService.stopSpeech();
        isSpeechIng = false;
        myWarkUpService.startWarkUp();
        //TODO 调界面隐藏 语音界面
        logger.i("onEndWarkUp success 调界面隐藏");
        SpeechDialogView.getInstance(MyApp.getInstance()).closeView();
    }

    @Override
    public void onResult(boolean isBaiduSpeech, String strResult) {
        logger.i("onResult=" + strResult);
        mHandler.removeMessages(END_WARKUP);
        mHandler.sendEmptyMessageDelayed(END_WARKUP,DELAY_END_WARKUP);
        Message msg;
        if (isBaiduSpeech) {
            msg = BaiduSpeechParserServiceImpl.getInstance().parserSpeechStr(strResult);
        } else {
            msg = XunfaiSpeechParserServiceImpl.getInstance().parserSpeechStr(strResult);
        }
        if (msg == null){
            return;
        }
        SpeechMessageBean speechMsg = new SpeechMessageBean();
        speechMsg.setWhat(msg.what);
        speechMsg.setArg1(msg.arg1);
        speechMsg.setArg2(msg.arg2);
        speechMsg.setObj(msg.obj.toString());
        try {
            //发语音消息给控制器
            if (mISpeechService != null){
                mISpeechService.controllerDeviceBySpeech(speechMsg);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (msg.obj != null){
            SpeechDialogView.getInstance(MyApp.getInstance()).setTextSpeech(msg.obj.toString());
        }
    }

    @Override
    public void onResultError() {

    }

    @Override
    public void onWarkUpReady() {
        if (isSpeechIng) {
            logger.i("onBaiduWarkup isSpeechIng return");
            return;
        }
        myWarkUpService.stopWarkUp();
        logger.i("onBaiduWarkup success ");
        isSpeechIng = true;
        mySpeechService.startSpeech();
    }
}
